<template>
  <div style="display: inline-block">
    <div class="phonebox">
      <div class="imgbox">
        <img :src="bookInfo.image" alt="" />
      </div>
      <div class="pricebox">
        {{ "￥" + bookInfo.price }}
      </div>
      <div class="directionbox">
        {{ bookInfo.description }}
      </div>
      <div class="sellbox">
        {{ bookInfo.comment }}
      </div>
      <div class="tag">
        <el-tag small type="danger">自营</el-tag>
      </div>
      <div class="smalltag">
        <i
          class="el-icon-shopping-cart-2 changeTag"
          @click="intocart(bookInfo)"
        ></i>
        <i
          v-if="!changeFlag"
          class="el-icon-star-off collect"
          @click="changeTag(bookInfo)"
        ></i>
        <i
          v-if="changeFlag"
          class="el-icon-star-on collect"
          @click="changeTag(bookInfo)"
        ></i>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  props: ["bookInfo"],
  data() {
    return {
      changeFlag: false,
      // bookBox 用于存放暂时收藏的数据
      bookBox: [],
    };
  },
  created() {},
  methods: {
    changeTag(book) {
      this.changeFlag = !this.changeFlag;
      if (this.changeFlag) {
        this.$emit("addBook", book);
      } else {
        // console.log(this.bookBox);
        // var bookBox2 = [];
        // bookBox2 = this.bookBox.filter((item) => {
        //   // console.log(item.id);
        //   // console.log(book.id);
        //   console.log(item.id == book.id);
        //   item.id != book.id;
        // }); //每一个组件内部都会有一个 bookBox 点击第一本书的时候 放到第一本书的bookBox中 点击第二本书时 放到第二本书的bookbox 中 所以不能统一管理
        this.$emit("deleteBook", book);
      }
    },
    intocart(book) {
      console.log(book);
      this.$http
        .post("/v1/home/addCart", {
          id: book.id,
          price: book.price,
          image: book.image,
        })
        .then((res) => {
          if (res.data.code === 1) {
            this.$message.success("已加入购物车");
            this.$store.commit("shopcart/addGoods", this.id);
          } else {
            // this.$message.warning("添加失败");
            console.log(res.data.code);
          }
        });
    },
  },
};
</script>
<style  lang='less' scoped>
.phonebox {
  cursor: pointer;
  display: flex;
  padding: 10px 0;
  width: 320px;
  height: 400px;
  box-sizing: border-box;
  margin-top: 20px;
  margin-bottom: 20px;
  margin-left: 10px;
  align-items: center;
  flex-direction: column;
}
.phonebox:hover {
  box-shadow: 0 0 3px #ccc;
}
img {
  width: 250px;
  height: 250px;
}
.pricebox {
  width: 220px;
  text-align: left;
  color: red;
  font-size: 22px;
  font-weight: 500;
}
.directionbox {
  width: 220px;
  font-size: 10px;
  text-align: left;
  margin-bottom: 10px;
  color: #2c3e50;
}
.sellbox {
  width: 220px;
  text-align: left;
  color: #646fb0;
}
.tag {
  width: 220px;
  text-align: left !important;
}
.changeTag {
  margin-right: 10px;
  color: rgb(64, 158, 255);
  font-size: 24px;
  font-weight: 800;
}

.smalltag {
  margin-left: 250px;
}
.collect {
  color: red;
  font-size: 24px;
  font-weight: 800;
}
</style>